package com.oa.ylms.apps.actions.move;

import java.util.List;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.move.IPersonMoveInfoService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.move.PersonMoveInfoValue;
import com.oa.ylms.apps.entity.organization.PostInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class PersonMoveInfoAction extends BasicAction {
private ISectionInfoService sectionService;
private IPersonMoveInfoService moveService;
private IPostInfoService postService;
private IStaffInfoService staffService;
private List<StaffInfoValue>staffList;
private List<SectionInfoValue>sectionList;
private List<PersonMoveInfoValue>moveList;
private List<PostInfoValue>postList;
private Integer submitState;
private String comments;
private Integer sectionId;
private Integer moveId;
private String str;
private String jsonStr;

//根据部门得到所有部门下的所有人
public String getPersonBySectionId() throws Exception{
	List<StaffInfoValue> staffList2 =staffService.queryStaffInfoBySectionId(sectionId);
	Gson obj = new Gson();
	this.str = obj.toJson(staffList2);
	response.setContentType("text/html; charset=UTF-8");
	response.setHeader("Cache-Control", "no-cache");
	response.getWriter().write(str);
	return null;
}
public String queryPostBySection()throws Exception{
	if (sectionId != null) {
		postList = postService.queryPostInfoBySectionId(sectionId);
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		this.str = getOptionsFrom(postList); // obj.toString();
		response.getWriter().print(str);
	}
	return null;
}
public String getOptionsFrom(List<PostInfoValue> list) {
	StringBuilder sb = new StringBuilder();
	for (PostInfoValue piv : list) {
		String temp = "<option value=\"" + piv.getPostId().toString()
				+ "\">" + piv.getPostName() + "</option>";

		sb.append(temp);
	}
	return sb.toString();
}
public String add(){
	staffInfo=(StaffInfoValue) session.get("login_staffInfo");
	JSONObject json =JSONObject.fromObject(jsonStr);
	String result=json.getString("result");
	int postId=json.getInt("postId");
	String comments=json.getString("comments");
	String result1=result.substring(0, result.length()-1);
	try {
		moveService.addPersonMoveInfo(postId, staffInfo, result1,comments);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
//获取所有的部门
public String getSection()throws Exception{
	sectionList=sectionService.queryAllSectionInfo();
	return "getSection";
}
//列出个人操作的人员调度申请信息
public String execute(){
	try {
		staffInfo=staffService.queryByStaffId(1);
		moveList=moveService.queryPersonMoveInfoWithPage(staffInfo, null, rows, currpage);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return SUCCESS;
}
//列出所有的人员调度申请信息
public String toManagerList() throws Exception{
	 moveList=moveService.queryPersonMoveInfoWithPage(staffInfo, null, rows, currpage);
	return "roManagerList";
}
//列出所有的需申请的人员调度申请信息
public String toCheckList()throws Exception{
	staffInfo=(StaffInfoValue) session.get("login_staffInfo");
	moveList=moveService.queryMoveCheck(staffInfo,rows,currpage);
	return "toCheckList";
}
//进行审批
public String check() {
	try {
		staffInfo=(StaffInfoValue) session.get("login_staffInfo");
		if (submitState == 1) {
			moveService.checkPass(staffInfo, moveId, new String(comments
					.getBytes("ISO-8859-1"), "UTF-8"));
		}
		if (submitState == 0) {
			moveService.checkUnpass(staffInfo, moveId, new String(comments
					.getBytes("ISO-8859-1"), "UTF-8"));
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "check";
}
public ISectionInfoService getSectionService() {
	return sectionService;
}
public void setSectionService(ISectionInfoService sectionService) {
	this.sectionService = sectionService;
}
public IPersonMoveInfoService getMoveService() {
	return moveService;
}
public void setMoveService(IPersonMoveInfoService moveService) {
	this.moveService = moveService;
}
public IStaffInfoService getStaffService() {
	return staffService;
}
public void setStaffService(IStaffInfoService staffService) {
	this.staffService = staffService;
}
public Integer getSectionId() {
	return sectionId;
}
public void setSectionId(Integer sectionId) {
	this.sectionId = sectionId;
}
public Integer getMoveId() {
	return moveId;
}
public void setMoveId(Integer moveId) {
	this.moveId = moveId;
}

public IPostInfoService getPostService() {
	return postService;
}
public void setPostService(IPostInfoService postService) {
	this.postService = postService;
}
public List<PostInfoValue> getPostList() {
	return postList;
}
public void setPostList(List<PostInfoValue> postList) {
	this.postList = postList;
}
public String getStr() {
	return str;
}
public void setStr(String str) {
	this.str = str;
}
public List<StaffInfoValue> getStaffList() {
	return staffList;
}
public void setStaffList(List<StaffInfoValue> staffList) {
	this.staffList = staffList;
}
public String getJsonStr() {
	return jsonStr;
}
public void setJsonStr(String jsonStr) {
	this.jsonStr = jsonStr;
}
public List<SectionInfoValue> getSectionList() {
	return sectionList;
}
public void setSectionList(List<SectionInfoValue> sectionList) {
	this.sectionList = sectionList;
}
public List<PersonMoveInfoValue> getMoveList() {
	return moveList;
}
public void setMoveList(List<PersonMoveInfoValue> moveList) {
	this.moveList = moveList;
}
public Integer getSubmitState() {
	return submitState;
}
public void setSubmitState(Integer submitState) {
	this.submitState = submitState;
}
public String getComments() {
	return comments;
}
public void setComments(String comments) {
	this.comments = comments;
}

}
